package org.despair2D.ui 
{
	import flash.display.DisplayObject;
	import flash.utils.getDefinitionByName;
	import org.despair2D.debug.Logger;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.core.Component;
	import org.despair2D.ui.core.ComponentProxy;
	import org.despair2D.ui.puppet.*;
	import org.despair2D.ui.core.OriginSprite;

	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
	/**
	 * 合体组
	 * 
	 * @usage	1. 定位中心点
	 * 			2. 布局
	 */
public class FusionGroup extends ComponentProxy implements IFusion
{
	
	public function FusionGroup()
	{
		m_reality = new Fusion()
		m_shell = OriginSprite.getOriginSprite(m_reality.displayObject)
	}
	
	
	/**
	 * 加入合体
	 * @param	fusion
	 * @param	name
	 */
	final public function addFusion( fusion:IFusion, name:String = null ) : void
	{
		if (fusion == this)
		{
			Logger.reportError(this, 'addFusion', '不可加入自身.');
		}
		
		if (!m_fusionList)
		{
			m_fusionList = [];
		}
		
		m_fusionList[m_numFusion++] = fusion;
		if (name == null)
		{
			name = 'fusion_' + m_numFusion;
		}
		
		Object(fusion).setGroup(this, name);
		m_reality.m_fusionComp.addChild(Object(fusion).displayObject);
	}
	
	/**
	 * 查找合体
	 * @param	name
	 */
	final public function retrieveFusion( name:String ) : IFusion
	{
		var l:int = m_numFusion;
		
		while (--l > -1)
		{
			if (m_fusionList[l].name == name) 
			{
				return m_fusionList[l];
			}
		}
		return null;
	}
	
	/**
	 * 削除全部合体
	 */
	final public function removeAllFusions() : void
	{
		while (m_numFusion > 0) 
		{
			m_fusionList[--m_numFusion].recycle();
		}
		m_fusionList = null;
	}
	
	/**
	 * 定位中心点
	 * @param	ox
	 * @param	oy
	 */
	final public function setOrigin( ox:Number, oy:Number ) : void
	{
		m_shell.setOrigin(ox,oy)
	}
	
	
	
	override public function get x () : Number { return m_shell.x; }
	override public function set x ( v:Number ) : void 
	{
		m_shell.x = v;
	}
	
	override public function get y () : Number{ return m_shell.y; }
	override public function set y ( v:Number ) : void 
	{
		m_shell.y = v;
	}
	
	override public function get rotation () : Number { return m_shell.rotation; }
	override public function set rotation ( v:Number ) : void 
	{
		m_shell.rotation = v;
	}
	
	override public function get scaleX () : Number { return m_shell.scaleX; }
	override public function set scaleX ( v:Number ) : void 
	{
		m_shell.scaleX = v;
	}
	
	override public function get scaleY () : Number { return m_shell.scaleY; }
	override public function set scaleY ( v:Number ) : void 
	{
		m_shell.scaleY = v;
	}
	
	
	final override public function move( tx:Number, ty:Number ) : void
	{
		m_shell.x = tx
		m_shell.y = ty
	}
	
	final override public function offset( tx:Number, ty:Number ) : void
	{
		m_shell.x += tx
		m_shell.y += ty
	}
	
	final public function get group() : FusionGroup
	{
		return m_reality.group;
	}
	
	final public function get numPuppets() : int
	{
		return m_reality.numPuppets
	}
	
	final public function createAnimePuppet( sectionName:String, index:int = -1, name:String = null ) : AnimePuppet
	{
		return m_reality.createAnimePuppet(sectionName, index, name);
	}
	
	final public function createImagePuppet( isCenter:Boolean = false, index:int = -1, name:String = null ) : ImagePuppet
	{
		return m_reality.createImagePuppet( isCenter, index, name);
	}
	
	final public function createNineScalePuppet( displayRef:*, width:Number = -1, height:Number = -1, index:int = -1, name:String = null ) : NineScalePuppet
	{
		return m_reality.createNineScalePuppet(displayRef, width, height, index, name);
	}
	
	final public function createDisplayObjectContainerPuppet( cacheAsBitmap:Boolean = true, index:int = -1, name:String = null ) : DisplayObjectContainerPuppet
	{
		return m_reality.createDisplayObjectContainerPuppet( cacheAsBitmap, index, name);
	}
	
	final public function createTextPuppet( text:String, offsetX:Number = 0, offsetY:Number = 0, embedFont:Boolean = false, index:int = -1, name:String = null ) : TextPuppet
	{
		return m_reality.createTextPuppet(text, offsetX, offsetY, embedFont, index, name);
	}
	
	final public function getPuppetByName( name:String ) : IPuppet 
	{
		return m_reality.getPuppetByName(name);
	}
	
	final public function getPuppetAt( index:int = 0 ) : IPuppet
	{
		return m_reality.getPuppetAt(index)
	}
	
	final public function removeAllPuppets() : void
	{
		m_reality.removeAllPuppets();
	}
	
	final override public function kill() : void
	{
		if (m_reality.m_group)
		{
			m_reality.m_group.removeProxy(this);
		}
		
		super.kill();
	}
	
	
	
	final ns_despair function makeTransform() : void
	{
		var l:int = m_numFusion;
		while (--l > -1)
		{
			m_fusionList[l].makeTransform();
		}
	}
	
	final ns_despair function removeProxy( fusion:IFusion ) : void
	{
		var index:int        =  m_fusionList.indexOf(fusion);
		m_fusionList[index]  =  m_fusionList[--m_numFusion];
		m_fusionList.pop();
	}
	
	override ns_despair function recycle() : void
	{
		this.removeAllFusions()
		super.recycle();
		m_reality = null
		m_shell.recycle()
		m_shell = null
	}
	
	final override ns_despair function get view() : IView
	{
		return m_reality;
	}

	final override public function get displayObject() : DisplayObject
	{
		return m_shell
	}
	
	final ns_despair function setGroup( v:FusionGroup, name:String ) : void
	{
		m_reality.setGroup(v, name)
	}
	
	

	ns_despair var m_fusionList:Array;
	
	ns_despair var m_numFusion:int;
	
	ns_despair var m_reality:Fusion

	ns_despair var m_shell:OriginSprite
	
}

}